﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using TConfig;

public class ExportSceneInfo {
	
	static int assetsLength = ("Assets/").Length;
	
	[MenuItem("Tools/Export Config/Current SceneObjs Info")]
	public static void ExportScene()
	{
		ExportSceneMapObj();
		ExportSceneMonsterObj();
	}
	
	[MenuItem("Tools/Export Config/Current Scene MapObjs Info")]
	public static void ExportSceneMapObj()
	{
		string path = Path.GetDirectoryName(Application.dataPath) + "/data/Scene/Map/";
		if(!Directory.Exists(path))
			Directory.CreateDirectory(path);
		
		GameObject[] sceneObjs = MonoBehaviour.FindObjectsOfType(typeof(GameObject)) as GameObject[];
		List<SceneObj> objList = new List<SceneObj>();
		string rootPath = "Assets/GameResources/UI/Dep/Sprite/Map/";
		
		foreach(GameObject sceneObj in sceneObjs)
		{
			switch(PrefabUtility.GetPrefabType(sceneObj))
			{
			case PrefabType.DisconnectedModelPrefabInstance:
			case PrefabType.DisconnectedPrefabInstance:
			case PrefabType.MissingPrefabInstance:
				Debug.LogWarning("[Prefab lose] : " + sceneObj.name + " is losed it's prefab!", sceneObj);
				break;
			}
			
			GameObject prefab = PrefabUtility.GetPrefabParent(sceneObj) as GameObject;
			if(prefab != null && prefab.layer == LayerMask.NameToLayer("Map"))
			{
				if(prefab.transform.parent == null)
				{
					Transform trans = sceneObj.transform;
					SceneObj obj = new SceneObj();
					obj.name = sceneObj.name;
					string assetPath = AssetDatabase.GetAssetPath(prefab);
					string assetPathExtension = Path.GetExtension(assetPath);
					assetPath = assetPath.Substring(0, assetPath.Length - assetPathExtension.Length) + UIManager.uiFitName;
					if(assetPath.StartsWith(rootPath))
					{
						obj.prefabPath = assetPath.Substring(assetsLength);
						obj.parentName = (trans.parent != null) ? trans.parent.name : "";
						obj.positionX = trans.localPosition.x;
						obj.positionY = trans.localPosition.y;
						obj.positionZ = trans.localPosition.z;
						obj.rotationX = trans.localRotation.eulerAngles.x;
						obj.rotationY = trans.localRotation.eulerAngles.y;
						obj.rotationZ = trans.localRotation.eulerAngles.z;
						obj.scaleX = trans.localScale.x;
						obj.scaleY = trans.localScale.y;
						obj.scaleZ = trans.localScale.z;
						objList.Add(obj);
					}
				}
			}
		}
		
		string str = EditorApplication.currentScene;
		string sceneName = Path.GetFileNameWithoutExtension(str);
		string savePath = path + sceneName + "_MapInfo.xml";
		XML<List<SceneObj>>.SaveAtPath(objList, savePath);
		
		Debug.Log(" Map 配表导出完成！ ");
	}
	
	[MenuItem("Tools/Export Config/Current Scene MonsterObjs Info")]
	public static void ExportSceneMonsterObj()
	{
		string path = Path.GetDirectoryName(Application.dataPath) + "/data/Scene/Monster/";
		if(!Directory.Exists(path))
			Directory.CreateDirectory(path);
		
		GameObject[] sceneObjs = MonoBehaviour.FindObjectsOfType(typeof(GameObject)) as GameObject[];
		List<SpriteObj> objList = new List<SpriteObj>();
		string rootPath = "Assets/GameResources/UI/Dep/Sprite/Monster/";
		
		foreach(GameObject sceneObj in sceneObjs)
		{
			switch(PrefabUtility.GetPrefabType(sceneObj))
			{
			case PrefabType.DisconnectedModelPrefabInstance:
			case PrefabType.DisconnectedPrefabInstance:
			case PrefabType.MissingPrefabInstance:
				Debug.LogWarning("[Prefab lose] : " + sceneObj.name + " is losed it's prefab!", sceneObj);
				break;
			}
			
			GameObject prefab = PrefabUtility.GetPrefabParent(sceneObj) as GameObject;
			if(prefab != null)
			{
				if(prefab.transform.parent == null)
				{
					Transform trans = sceneObj.transform;
					SpriteObj obj = new SpriteObj();
					obj.name = sceneObj.name;
					string assetPath = AssetDatabase.GetAssetPath(prefab);
					string assetPathExtension = Path.GetExtension(assetPath);
					assetPath = assetPath.Substring(0, assetPath.Length - assetPathExtension.Length) + UIManager.uiFitName;
					if(assetPath.StartsWith(rootPath))
					{
						obj.prefabPath = assetPath.Substring(rootPath.Length);
						obj.prefabPath = assetPath.Substring(assetsLength);
						obj.parentName = (trans.parent != null) ? trans.parent.name : "";
						obj.positionX = trans.localPosition.x;
						obj.positionY = trans.localPosition.y;
						obj.positionZ = trans.localPosition.z;
						obj.rotationX = trans.localRotation.eulerAngles.x;
						obj.rotationY = trans.localRotation.eulerAngles.y;
						obj.rotationZ = trans.localRotation.eulerAngles.z;
						obj.scaleX = trans.localScale.x;
						obj.scaleY = trans.localScale.y;
						obj.scaleZ = trans.localScale.z;
						
						TMonster mon = trans.GetComponent<TMonster>();
						
						if(mon != null)
						{
							obj.hp = mon.Hp;
							obj.mp = mon.Mp;
							obj.attack = mon.AttackValue;
							obj.attackType = (int)mon.AttackType;
							obj.attackDistance = mon.AttackDistance;
							obj.attackFrequency = mon.AttackFrequency;
							obj.defense = mon.DefenseValue;
							obj.speedX = mon.SpeedX;
							obj.speedY = mon.SpeedY;
							obj.xPingPongLength = mon.XPingPongLength;
							obj.yStopPos = mon.YStopPos;
							obj.bulletId = mon.BulletId;
							obj.moveType = (int)mon.MoveType;
						}
						
						objList.Add(obj);
					}
				}
			}
		}
		
		string str = EditorApplication.currentScene;
		string sceneName = Path.GetFileNameWithoutExtension(str);
		string savePath = path + sceneName + "_MonsterInfo.xml";
		XML<List<SpriteObj>>.SaveAtPath(objList, savePath);
		
		Debug.Log(" Monster 配表导出完成！ ");
	}
	
	[MenuItem("Tools/Export Config/Hero Objs Info")]
	public static void ExportHeroObj()
	{
		
		string path = Path.GetDirectoryName(Application.dataPath) + "/data/Scene/Hero/";
		if(!Directory.Exists(path))
			Directory.CreateDirectory(path);
		
		string rootPath = "Assets/GameResources/UI/Dep/Sprite/Hero/";
		string[] files = Directory.GetFiles(rootPath,
			"*", SearchOption.AllDirectories);
		List<SpriteObj> objList = new List<SpriteObj>();
		
		foreach(string file in files)
		{
			if(Path.GetExtension(file) == ".meat" || Path.GetFullPath(file).Contains(".svn"))
				continue;
			
			Debug.Log("开始创建Hero xml配表数据:" + file);
			
			Object obj = AssetDatabase.LoadMainAssetAtPath(file);
			
			if(obj != null)
			{
				GameObject gobj = obj as GameObject;
				if(gobj != null)
				{
					string assetPath = AssetDatabase.GetAssetPath(gobj);
					string assetPathExtension = Path.GetExtension(assetPath);
					assetPath = assetPath.Substring(0, assetPath.Length - assetPathExtension.Length) + UIManager.uiFitName;
					if(assetPath.StartsWith(rootPath))
					{
						SpriteObj teamObj = new SpriteObj();
						teamObj.name = gobj.name;
						Transform trans = gobj.transform;
						teamObj.prefabPath = assetPath.Substring(rootPath.Length);
						teamObj.prefabPath = assetPath.Substring(assetsLength);
						teamObj.parentName = (trans.parent != null) ? trans.parent.name : "";
						teamObj.positionX = trans.localPosition.x;
						teamObj.positionY = trans.localPosition.y;
						teamObj.positionZ = trans.localPosition.z;
						teamObj.rotationX = trans.localRotation.eulerAngles.x;
						teamObj.rotationY = trans.localRotation.eulerAngles.y;
						teamObj.rotationZ = trans.localRotation.eulerAngles.z;
						teamObj.scaleX = trans.localScale.x;
						teamObj.scaleY = trans.localScale.y;
						teamObj.scaleZ = trans.localScale.z;
						
						TTeam team = trans.GetComponent<TTeam>();
						
						if(team != null)
						{
							teamObj.hp = team.Hp;
							teamObj.mp = team.Mp;
							teamObj.attack = team.AttackValue;
							teamObj.attackType = (int)team.AttackType;
							teamObj.attackDistance = team.AttackDistance;
							teamObj.attackFrequency = team.AttackFrequency;
							teamObj.defense = team.DefenseValue;
							teamObj.speedX = team.SpeedX;
							teamObj.speedY = team.SpeedY;
							teamObj.xPingPongLength = team.XPingPongLength;
							teamObj.yStopPos = team.YStopPos;
							teamObj.bulletId = team.BulletId;
							teamObj.moveType = (int)team.MoveType;
						}
						
						objList.Add(teamObj);
					}
				}
			}
		}
		string savePath = path + "HeroInfo.xml";
		XML<List<SpriteObj>>.SaveAtPath(objList, savePath);
		Debug.Log(" Hero 配表导出完成！ ");
	}
	
	[MenuItem("Tools/Export Config/Bullets Info")]
	public static void ExportBulletObj()
	{
		string path = Path.GetDirectoryName(Application.dataPath) + "/data/Scene/Bullet/";
		if(!Directory.Exists(path))
			Directory.CreateDirectory(path);
		
		string rootPath = "Assets/GameResources/UI/Dep/Sprite/Bullet/";
		string[] files = Directory.GetFiles(rootPath,
			"*", SearchOption.AllDirectories);
		List<BulletObj> objList = new List<BulletObj>();
		
		foreach(string file in files)
		{
			if(Path.GetExtension(file) == ".meat" || Path.GetFullPath(file).Contains(".svn"))
				continue;
			
			Debug.Log("开始创建子弹xml配表数据:" + file);
			
			Object obj = AssetDatabase.LoadMainAssetAtPath(file);
			
			if(obj != null)
			{
				GameObject gobj = obj as GameObject;
				if(gobj != null)
				{
					string assetPath = AssetDatabase.GetAssetPath(gobj);
					string assetPathExtension = Path.GetExtension(assetPath);
					assetPath = assetPath.Substring(0, assetPath.Length - assetPathExtension.Length) + UIManager.uiFitName;
					if(assetPath.StartsWith(rootPath))
					{
						Transform trans = gobj.transform;
						BulletObj bulletObj = new BulletObj();
						TBullet bullet = trans.GetComponent<TBullet>();
						if(bullet != null)
						{
							bulletObj.filePath = assetPath.Substring(assetsLength);
							bulletObj.id = bullet.Id;
							bulletObj.speed = bullet.MoveSpeed;
							bulletObj.bulletType = (int)bullet.BulletType;
							bulletObj.moveType = (int)bullet.BulletMoveType;
							
							objList.Add(bulletObj);
						}
					}
				}
			}
		}
		string savePath = path + "BulletInfo.xml";
		XML<List<BulletObj>>.SaveAtPath(objList, savePath);
		
		
//		GameObject[] sceneObjs = MonoBehaviour.FindObjectsOfType(typeof(GameObject)) as GameObject[];
//		List<BulletObj> objList = new List<BulletObj>();
//		string rootPath = "Assets/GameResources/UI/Dep/Sprite/Bullet/";
//		
//		foreach(GameObject sceneObj in sceneObjs)
//		{
//			switch(PrefabUtility.GetPrefabType(sceneObj))
//			{
//			case PrefabType.DisconnectedModelPrefabInstance:
//			case PrefabType.DisconnectedPrefabInstance:
//			case PrefabType.MissingPrefabInstance:
//				Debug.LogWarning("[Prefab lose] : " + sceneObj.name + " is losed it's prefab!", sceneObj);
//				break;
//			}
//			
//			GameObject prefab = PrefabUtility.GetPrefabParent(sceneObj) as GameObject;
//			if(prefab != null)
//			{
//				if(prefab.transform.parent == null)
//				{
//					Transform trans = sceneObj.transform;
//					BulletObj obj = new BulletObj();
//					obj.name = sceneObj.name;
//					string assetPath = AssetDatabase.GetAssetPath(prefab);
//					string assetPathExtension = Path.GetExtension(assetPath);
//					assetPath = assetPath.Substring(0, assetPath.Length - assetPathExtension.Length) + UIManager.uiFitName;
//					if(assetPath.StartsWith(rootPath))
//					{
//						
//						TBullet bullet = trans.GetComponent<TBullet>();
////						obj.id
//					}
//				}
//			}
//		}
//		
//		string str = EditorApplication.currentScene;
//		string sceneName = Path.GetFileNameWithoutExtension(str);
//		string savePath = path + sceneName + "_BulletInfo.xml";
//		XML<List<BulletObj>>.SaveAtPath(objList, savePath);
		
		Debug.Log(" Bullet 配表导出完成！ " + savePath);
	}
	
//	static void BuildAllAssetBundles(string fileFolder, string saveFolder)
//	{
//		string root = Directory.GetCurrentDirectory() + "/data/";
//		char[] separatorCharArray = {'/', '\\'};
//		
//		fileFolder = fileFolder.TrimEnd(separatorCharArray);
//		saveFolder = saveFolder.TrimEnd(separatorCharArray);
//		string[] files = Directory.GetFiles(fileFolder, "*", SearchOption.AllDirectories);
//		foreach(string file in files)
//		{
//			if(Path.GetExtension(file) == ".meat" || Path.GetFullPath(file).Contains(".svn"))
//				continue;
//			
//			Debug.Log("开始打包:" + file);
//			
//			Object obj = AssetDatabase.LoadMainAssetAtPath(file);
//			
//			if(obj != null)
//			{
//				string saveFileDirectory = saveFolder + Path.GetDirectoryName(file).Substring(fileFolder.Length);
//				string saveFileName = Path.GetFileNameWithoutExtension(file);
//				string saveFileExtension = resourceExtension;
//				string saveFilePath = Path.Combine(saveFileDirectory, saveFileName + saveFileExtension);
//				
//				if(!Directory.Exists(saveFileDirectory))
//					Directory.CreateDirectory(saveFileDirectory);
//				
//				try
//				{
//					if(BuildPipeline.BuildAssetBundle(obj, null, saveFilePath, BuildAssetBundleOptions.CollectDependencies
//					| BuildAssetBundleOptions.CompleteAssets | BuildAssetBundleOptions.UncompressedAssetBundle,
//						BuildTarget.StandaloneWindows))
//					{
//						string newPath = saveFileDirectory + "/" + saveFileName + saveFileExtension;
//						EncodeFile(saveFilePath, newPath);
//					}
//				}
//				catch(Exception e)
//				{
//					Debug.Log(e);
//					Debug.LogException(e);
//				}
//				
//			}
//			else
//			{
//				Debug.Log("不可识别的资源:" + file);
//			}
//		}
//	}
}
